package org.learn.java;

/**
 * Longest Plateau Problem
 * <p>
 * Given an array of integers, find the length and location of the longest
 * contiguous sequence of equal values where the values of the elements just
 * before and just after this sequence are smaller
 * </p>
 * 
 * @author mohit
 * 
 *         </p> e.g.<br>
 *         java LongestPlateau 40 0 1 1 1 1 0 1 2 2 2 2 2 2 0 0 0 1 1 1 1 1 1 1
 *         1 1 0 6 6 6 6 6 6 6 6 6 6 6 6 6 4</p>
 */
public class LongestPlateau {

	public static void main(String[] args) {
		int N = Integer.parseInt(args[0]);
		int a[] = new int[N];
		int maxL, maxPos;
		maxL = maxPos = 0;
		// Input data from command line
		for (int i = 0; i < N; i++)
			a[i] = Integer.parseInt(args[i + 1]);

		for (int i = 0; i < N; i++)
			System.out.printf("%d ", a[i]);

		System.out.println();

		int i = 1;
		int l = 1;
		while (i < N - 2) {
			int key = a[i];
			if (key > a[i - 1]) {
				l = 1;
				for (int j = i + 1; j < N - 1; j++) {
					if (key == a[j])
						l++;
					else
						break;
				}
				if (l > 1 && a[i + l] < key && l > maxL) {
					maxL = l;
					maxPos = i;
				}
				i += l;
			} else
				i++;
		}

		if (maxL > 1)
			System.out.printf("Length: %d Position: %d\n", maxL, maxPos);
		else
			System.out.printf("no Longest Plateau found \n");
	}
}
